//
//  DZRedomNumController.m
//  dzyc
//
//  Created by 蓝鳍互娱 on 2024/6/12.
//

#import "DZRedomNumController.h"
#import "DZMainController.h"
#import "DZRedomNumCell.h"
#import "DZRedomAlertView.h"
@interface DZRedomNumController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *redomNumBtn;
@property (weak, nonatomic) IBOutlet UICollectionView *redomNumCollection;
@property (nonatomic,copy) NSString *redomNum;//随机数位数
@property (nonatomic,copy) NSString *redomType;//1.数字 2.英文 3.英文和数字
@property (nonatomic,assign) BOOL isRepert;//是否重复
@property (nonatomic,assign) NSInteger redomAllNum;//随机数量

@property (nonatomic,strong) NSMutableArray *redomAllNums;
@end

@implementation DZRedomNumController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    self.redomNumCollection.delegate = self;
    self.redomNumCollection.dataSource = self;
    [self.redomNumCollection registerClass:[DZRedomNumCell class] forCellWithReuseIdentifier:@"DZRedomNumCell"];
    self.redomNumBtn.layer.cornerRadius = DZRatio(6);
    self.redomNumBtn.clipsToBounds = YES;
    
    self.redomNum = @"3";
    self.redomType = @"1";
    self.isRepert = false;
    self.redomAllNum = 30;
    
    [self updataRedomResult];
}
- (void)updataRedomResult{
    self.redomAllNums = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.redomAllNum ; i++) {
        if([self.redomType isEqualToString:@"1"]){//1.数字
            [self.redomAllNums addObject:[self makeRandomNum:[self.redomNum intValue]]];
        }else if([self.redomType isEqualToString:@"2"]){//2.英文
            [self.redomAllNums addObject:[self makeRandomLetterNum:[self.redomNum intValue]]];
        }else if([self.redomType isEqualToString:@"3"]){//3.英文和数字
            [self.redomAllNums addObject:[self makeRandomAllNum:[self.redomNum intValue]]];
        }
    }
    [self.redomNumCollection reloadData];
}
// 生成一个随机数数字
- (NSString *)makeRandomNum:(int)randomNum{
    int startNum = 9*(pow(10, randomNum-1));
    int addNum = 1*(pow(10, randomNum-1));
    int arcRandomNum = arc4random_uniform(startNum) + addNum;
    NSString *randomNumber = [NSString stringWithFormat:@"%d", arcRandomNum];
    if (!self.isRepert) {
        if([self.redomAllNums containsObject:randomNumber]){
            return [self makeRandomNum:randomNum];
        }
    }
    return randomNumber;
}
// 生成一个随机数字母
- (NSString *)makeRandomLetterNum:(int)randomNum{
    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *randomNumber = [NSMutableString stringWithCapacity:randomNum];
    for (int i = 0; i < randomNum; i++) {
        [randomNumber appendFormat:@"%C", [letters characterAtIndex:arc4random_uniform((uint32_t)[letters length])]];
    }
    if (!self.isRepert) {
        if([self.redomAllNums containsObject:randomNumber]){
            return [self makeRandomLetterNum:randomNum];
        }
    }
    return randomNumber;
}
// 生成一个随机数字母和数字
- (NSString *)makeRandomAllNum:(int)randomNum{
    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    NSMutableString *randomNumber = [NSMutableString stringWithCapacity:randomNum];
    for (int i = 0; i < randomNum; i++) {
        [randomNumber appendFormat:@"%C", [letters characterAtIndex:arc4random_uniform((uint32_t)[letters length])]];
    }
    if (!self.isRepert) {
        if([self.redomAllNums containsObject:randomNumber]){
            return [self makeRandomLetterNum:randomNum];
        }
    }
    return randomNumber;
}
- (IBAction)becomeRedomNum:(UIButton *)sender {
    [self updataRedomResult];
}
- (IBAction)redomDel:(UIButton *)sender {
    self.redomAllNums = [[NSMutableArray alloc] init];
    [self.redomNumCollection reloadData];
}
- (IBAction)redomSet:(UIButton *)sender {
    DZWeakSelf
    [DZRedomAlertView showRedomAlertView:self.redomNum redomType:self.redomType isRepert:self.isRepert complaint:^(NSString * _Nonnull redomNum, NSString * _Nonnull redomType, BOOL isRepert) {
        weakSelf.redomNum = redomNum;
        weakSelf.redomType = redomType;
        weakSelf.isRepert = isRepert;
        [weakSelf updataRedomResult];
    }];
}
- (IBAction)navBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)navSet:(UIButton *)sender {
    DZMainController * mainCtrl = [[DZMainController alloc] init];
    [self.navigationController pushViewController:mainCtrl animated:YES];
}
#pragma mark - UICollectionViewDelegateFlowLayout method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.redomAllNums.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (DZScreen_W - DZRatio(126))/2;
    CGSize itemSize = CGSizeMake(width, width/312*68);
    return itemSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return DZRatio(10);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return DZRatio(10);
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DZRedomNumCell *redomNumCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DZRedomNumCell" forIndexPath:indexPath];
    redomNumCell.redomNum = self.redomAllNums[indexPath.row];
    return redomNumCell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *redomNum = self.redomAllNums[indexPath.row];
    if(redomNum.length){
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = redomNum;
        [DzAlertHelper showMessage:@"复制成功"];
    }
}
@end
